<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;
class DayController extends Controller
{
    
    //返回日收入明细 
    public function ajaxIndex()
    {
        
        
        
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        
        
      
      $time = time();
      $time = '2018-10-18';
       //全部差存进数据库
     /*   $sql  = "SELECT bar,SUM(service) as service_count,SUM(amount) as amount_count, SUM(ticheng) as ticheng_count, SUM(activity) as activity_count,SUM(other) as other_count
        FROM income
        WHERE
	time = '".$time."'  GROUP BY bar";
        $list = DB::select($sql);//查询出来每个巴一天收入明细 
        
        $list = json_encode($list, true);
        $list = json_decode($list, true);
     
        
        foreach($list as &$v){
           $v['count']  = $v['service_count']+$v['ticheng_count']+$v['activity_count']+$v['other_count'];
           $v['beizhu'] = '日收入明细';
           $v['created_at'] = date('Y-m-d H:i:s');
           $v['time'] = $time;
           
           $info =  DB::table('two_level_bar')->insert([
               ['bar' => $v['bar'], 'service' => $v['service_count'],'amount'=>$v['amount_count'],'ticheng'=>$v['ticheng_count'],'activity'=>$v['activity_count'],
             'other'=>$v['other_count'],'count'=>$v['count'],'beizhu'=>$v['beizhu'],'created_at'=>$v['created_at'],'time'=>$v['time']],
              
           ]);
            
        }
        
 
        //一级巴
         $sql  = "SELECT project,SUM(service) as service_count,SUM(amount) as amount_count, SUM(ticheng) as ticheng_count, SUM(activity) as activity_count,SUM(other) as other_count
        FROM income
        WHERE
	time = '".$time."'  GROUP BY project";
        $list = DB::select($sql);//查询出来每个巴一天收入明细 
        
        $list = json_encode($list, true);
        $list = json_decode($list, true);
      
      
         foreach($list as &$v){
           $v['count']  = $v['service_count']+$v['ticheng_count']+$v['activity_count']+$v['other_count'];
           $v['beizhu'] = '一级巴收入';
           $v['created_at'] = date('Y-m-d H:i:s');
           $v['time'] = $time;
           
            $info =  DB::table('one_level_bar')->insert([
               ['project' => $v['project'], 'service' => $v['service_count'],'amount'=>$v['amount_count'],'ticheng'=>$v['ticheng_count'],'activity'=>$v['activity_count'],
              'other'=>$v['other_count'],'count'=>$v['count'],'beizhu'=>$v['beizhu'],'created_at'=>$v['created_at'],'time'=>$v['time']],
              
            ]);
            
        }
      echo "<pre>";
      var_dump($list);exit();*/
      

        
        //二级巴日报表
        //查询出来每个巴的 每日报表  还查薪资  房租水电费 
        $sql  = "SELECT bar,SUM(service+amount+ticheng+activity+other) count
        FROM income
        WHERE
	time = '".$time."' and type = 1  GROUP BY bar";
        $list = DB::select($sql);//查询出来每个巴一天收入明细 
        $list = json_encode($list, true);
        $list = json_decode($list, true);
       
        //外部收入
        $sql  = "SELECT bar,SUM(service+amount+ticheng+activity+other) as waibu_count
        FROM income
        WHERE
	time = '".$time."' and type =2  GROUP BY bar";
        $wincome = DB::select($sql);//查询出来每个巴一天收入明细 
        $wincome = json_encode($wincome, true);
        $wincome = json_decode($wincome, true);

        //其他收入
        $sql  = "SELECT bar,SUM(service+amount+ticheng+activity+other) as waibu_count
        FROM income
        WHERE
	time = '".$time."' and type =3  GROUP BY bar";
        $nincome = DB::select($sql);//查询出来每个巴一天收入明细 
        $nincome = json_encode($nincome, true);
        $nincome = json_decode($nincome, true);
       
        
         //固定薪酬
        $sql  = "SELECT bar,pingjun
        FROM 	price_users
	where is_b  = 1";
        $price = DB::select($sql);//查询出来每个巴一天收入明细 
        $price = json_encode($price, true);
        $price = json_decode($price, true);
        
        //其他收入
        $sql  = "SELECT bar,SUM(price) as price
        FROM money
        WHERE
	time = '".$time."'  GROUP BY bar";
        $money = DB::select($sql);//查询出来每个巴一天收入明细 
        $money = json_encode($money, true);
        $money = json_decode($money, true);
   
        
        $array  = array();
        $f = array();
        foreach($price as  $y){
        
            foreach($money as $v){
            
               if($y['bar']==$v['bar']){
                 
                    $f[] = $v['bar'];
                    $array[$v['bar']]['bar'] = $y['bar'];
                    $array[$v['bar']]['price'] = $v['price'];
                    $array[$v['bar']]['money'] = $y['pingjun'];
                    $array[$v['bar']]['fzsdf'] = '500';
                    $array[$v['bar']]['jian_count'] = $y['bar']+$v['price']+$y['pingjun']+'500';
        
                }else{
                    if(!in_array($v['bar'], $f)){
                            $array[$v['bar']]['bar'] = $v['bar'];
                            $array[$v['bar']]['price'] = $v['price'];
                            $array[$v['bar']]['money'] =  $y['pingjun'];
                            $array[$v['bar']]['fzsdf'] = '500';
                            $array[$v['bar']]['jian_count'] = $y['bar']+$v['price']+$y['pingjun']+'500';
                            
                    }
                    
                    if(!in_array($y['bar'], $f)){
                            $array[$y['bar']]['bar'] = $y['bar'];
                            $array[$y['bar']]['price'] = $v['price'];
                            $array[$y['bar']]['money'] = $y['pingjun'];
                            $array[$y['bar']]['fzsdf'] = '500';    
                            $array[$y['bar']]['jian_count'] = $y['bar']+$v['price']+$y['pingjun']+'500';
                            
                    }
                }      
            }
        }
        
      
        

        //将其他收入 和 外部收入 相加 
        //新增数组 
         //查询出来日收入报表  然后存进数据库
        $datas = array();
        $flag_bar = array();
        foreach($wincome as $y){
            foreach($nincome as $v){
                
                if($v['bar']==$y['bar']){
                    $flag_bar[] = $v['bar'];
                    $datas[$y['bar']]['bar'] = $y['bar'];
                    $datas[$y['bar']]['count'] = $v['waibu_count']+$y['waibu_count'];
                    
        
                }else{
                    if(!in_array($v['bar'], $flag_bar)){
                        $datas[$v['bar']] = $v;
                    }
                    
                    if(!in_array($y['bar'], $flag_bar)){
                        
                        $datas[$y['bar']] = $y;
                    }
                }
            }
        }
        
        //查询出来日收入报表  然后存进数据库
        $data = array();
        $flas_bar = array();
         foreach($list as $y){
             foreach($datas as $k=>$v){
                    if($v['bar']==$y['bar']){
                        $flas_bar[] = $v['bar'];
                        $data[$y['bar']]['bar'] = $y['bar'];
                        $data[$y['bar']]['count'] = $v['waibu_count']+$y['count'];
                        $data[$y['bar']]['neibu'] = $y['count'];
                        $data[$y['bar']]['waibu'] = $v['waibu_count'];
                    }else{
                          if(!in_array($v['bar'], $flas_bar)){
                            $data[$v['bar']]['bar'] = $v['bar'];
                            $data[$v['bar']]['count'] = $v['waibu_count']+$y['count'];
                            $data[$v['bar']]['neibu'] = $y['count'];
                            $data[$v['bar']]['waibu'] = $v['waibu_count'];
                    }
                        if(!in_array($y['bar'], $flas_bar)){
                            $data[$y['bar']]['bar'] = $y['bar'];
                            $data[$y['bar']]['count'] = $v['waibu_count']+$y['count'];
                            $data[$y['bar']]['neibu'] = $y['count'];
                            $data[$y['bar']]['waibu'] = $v['waibu_count'];
                           
                           
                        }
                    }
                
             }
         }

         $count = array();
         $coun = array();
         foreach($array as $k=>$y){
             foreach($data as $k=>$v){
                
                 
                     if($v['bar']==$y['bar']){
                         
                        $coun[] = $v['bar'];
                        $count[$y['bar']]['bar'] = $y['bar'];
                        $count[$y['bar']]['jia_count'] = $v['count'];
                        $count[$y['bar']]['neibu'] = $v['neibu'];
                       
                        $count[$y['bar']]['waibu'] = $v['waibu'];
                        $count[$y['bar']]['price'] = $y['price'];
                        $count[$y['bar']]['money'] = $y['money'];
                        $count[$y['bar']]['fzsdf'] = $y['fzsdf'];
                        $count[$y['bar']]['jian_count'] = $y['jian_count'];
                         $count[$y['bar']]['amiba'] = $y['jian_count']-$v['count'];
                    }else{
                          if(!in_array($v['bar'], $flas_bar)){
                         
                            $count[$v['bar']]['bar'] = $y['bar'];
                            $count[$v['bar']]['jia_count'] = $v['count'];
                            $count[$v['bar']]['neibu'] = $v['neibu'];
                            $count[$v['bar']]['waibu'] = $v['waibu'];
                            $count[$v['bar']]['price'] = $y['price'];
                            $count[$v['bar']]['money'] = $y['money'];
                            $count[$v['bar']]['fzsdf'] = $y['fzsdf'];
                            $count[$v['bar']]['jian_count'] = $y['jian_count'];
                            $count[$v['bar']]['amiba'] = $y['jian_count']-$v['count'];
                      }
                    
                        if(!in_array($y['bar'], $flas_bar)){
                               
                               $count[$y['bar']]['bar'] = $y['bar'];
                               $count[$y['bar']]['jia_count'] = $v['count'];
                               $count[$y['bar']]['neibu'] = $v['neibu'];
                               $count[$y['bar']]['waibu'] = $v['waibu'];
                               $count[$y['bar']]['price'] = $y['price'];
                               $count[$y['bar']]['money'] = $y['money'];
                               $count[$y['bar']]['fzsdf'] = $y['fzsdf'];
                               $count[$y['bar']]['jian_count'] = $y['jian_count'];
                               $count[$y['bar']]['amiba'] = $y['jian_count']-$v['count'];
                        }
                 }
             }
         }
         
        
      //获取当前月多少天数  然后根据每个巴 
      /* $days = cal_days_in_month(CAL_GREGORIAN, 10, 2018);
     
  
     
       $day = array();
       foreach($price as $K=>$v){
        
           for($i=1;$i<=8;$i++){
               if($v['bar']==$i){
                   
                   $day[$v['bar']] = round($v['count']/$days,2);
                   
               }
           }
       } */
       
       

       
        
   
       
       
              
      
                
           
                
                
                
            
        
        
        
        
        
        
        
        
                
                
          
        
                
        //一级巴汇总报表明细  查询内部收入   未完成  还查插入表  还查 固定薪资  房租水电费 
        $sql  = "SELECT type,project,SUM(service+amount+ticheng+activity+other) as neibu
        FROM 	income
        where time = '".$time."' and type =1
	GROUP BY project";
        $shanghai = DB::select($sql);//查询出来每个巴一天收入明细 
        $shanghai = json_encode($shanghai, true);
        $shanghai = json_decode($shanghai, true);

       
        //查询外部收入    存入外部收入总表   然后根据时间来合并  
        $sql  = "SELECT type,project,SUM(service+amount+ticheng+activity+other) as waibu
        FROM 	income
        where time = '".$time."' and type !=1
	GROUP BY project";
        $yunnan = DB::select($sql);//查询出来每个巴一天收入明细 
        $yunnan = json_encode($yunnan, true);
        $yunnan = json_decode($yunnan, true);
        
         
        //查询其他支出明细 1是上海亿大
        $sql  = "SELECT project,SUM(price) as zhichu
        FROM 	money
        where time = '".$time."' 
	GROUP BY project";
        $two_money = DB::select($sql);//查询出来每个巴一天收入明细 
        $two_money = json_encode($two_money, true);
        $two_money = json_decode($two_money, true);
    
       
       
    
    
        
     
        
        
        //一级巴汇总报表固定薪资 每天平均固定薪资
        $sql = "SELECT type,SUM(pingjun) as count
        FROM 	price_users
        where is_b = 1
	GROUP BY type";
        
        $xinzi = DB::select($sql);//查询出来每个巴一天收入明细 
        $xinzi = json_encode($xinzi, true);
        $xinzi = json_decode($xinzi, true);
        
        
        //房租水电费
        $sql="SELECT type,SUM(fz+sdf) AS count FROM
	common WHERE  type = 1 GROUP BY type";
       
        $countsdf = DB::select($sql);//查询出来每个巴一天收入明细 
        $countsdf = json_encode($countsdf, true);
        $countsdf = json_decode($countsdf, true);
        
        
        //查询出来日收入报表  然后存进数据库
        $count_shanghai = array();
        $count_sh = array();
         foreach($shanghai as $y){
             foreach($two_money as $k=>$v){
                    if($v['project']==$y['project']){
                       $count_sh[] = $v['project'];
                       $count_shanghai[$v['project']]['type']= $y['type'];
                       $count_shanghai[$v['project']]['neibu']= $y['neibu'];
                       $count_shanghai[$v['project']]['zhichu']= $v['zhichu'];
                       $count_shanghai[$v['project']]['fzsdf']= '12000';
                       $count_shanghai[$v['project']]['xinzi']= '500';
                       $count_shanghai[$v['project']]['zhichu_count']= $v['zhichu']+'12000'+'500';
                       
                               
                    }else{
                          if(!in_array($v['project'], $count_sh)){
                                 $count_shanghai[$v['project']]['type']= $y['type'];
                                 $count_shanghai[$v['project']]['zhichu']= $v['zhichu'];
                                 $count_shanghai[$v['project']]['neibu']= $y['neibu'];
                                 $count_shanghai[$v['project']]['fzsdf']= '12000';
                                 $count_shanghai[$v['project']]['xinzi']= '500';
                                 $count_shanghai[$v['project']]['zhichu_count']= $v['zhichu']+'12000'+'500';
                         }
                         
                        if(!in_array($y['project'], $count_sh)){
                               $count_shanghai[$y['project']]['type']= $y['type'];
                               $count_shanghai[$y['project']]['zhichu']= $v['zhichu'];
                               $count_shanghai[$y['project']]['neibu']= $y['neibu'];
                               $count_shanghai[$y['project']]['fzsdf']= '12000';
                               $count_shanghai[$y['project']]['xinzi']= '500';
                               $count_shanghai[$y['project']]['zhichu_count']= $v['zhichu']+'12000'+'500';
                           
                        }
                    }
                
             }
         }
         echo "<hr>";
        echo "<pre>";
        var_dump($count_shanghai);exit();
    
        
        
           
                
        
        
        
            
         $time = time();
         $time = date('Y-m-d');//获取当前时间
          
         $info =  DB::table('two_level_bar')->where('time','=',$time)->get();//查询当前时间日收入明细
         
         $list = DB::table('one_level_bar')->where('time','=',$time)->where('project','=',1)->get();
     
         // return view('admin.day.index');
        
    }

    
   

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
